//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.topvision.myviewtest.well.camera;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.hardware.Camera;
import android.hardware.Camera.Area;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.hardware.Camera.Size;
import android.os.Build;
import android.os.Build.VERSION;
import android.view.Display;
import android.view.WindowManager;
import com.honeywell.misc.HSMLog;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

public class CameraManager {
    private static CameraManager instance = null;
    private static int DECODES_PER_AF = 8;
    private Context appContext;
    private Boolean useFrontFacingCamera = Boolean.valueOf(false);
    private Boolean isCameraOpen = Boolean.valueOf(false);
    private Boolean isFlashEnabled = Boolean.valueOf(false);
    private byte[] baLastImage = null;
    private int openCameraIndex = -1;
    private int lastImageWidth;
    private int lastImageHeight;
    private int rearFacingCameraIndex = -1;
    private int frontFacingCameraIndex = -1;
    private Size currentPreviewSize = null;
    private Camera camera;
    private PictureCallback fullPictureCallback = null;
    public boolean keepCameraInitialized = true;
    public boolean isAutoFocusSupported = false;
    private boolean isCameraDefaulted = false;
    private Parameters savedFrontCameraSettings;
    private Parameters savedRearCameraSettings;
    public byte[] previewBuffer = new byte[3110400];

    public static CameraManager getInstance(Context context) {
        if(instance == null) {
            instance = new CameraManager(context);
        }

        return instance;
    }

    public static void destroyInstance() {
        HSMLog.trace();
        if(instance != null) {
            instance.closeCamera();
            instance = null;
        }

    }

    private CameraManager(Context context) {
        HSMLog.trace();
        this.appContext = context;
        this.openCamera();
    }

    public Boolean isCameraOpen() {
        return this.isCameraOpen;
    }

    public int getOpenCameraIndex() {
        return this.openCameraIndex;
    }

    public Camera getCamera() {
        return this.camera;
    }

    public void setDecoderAttemptsPerAFCycle(int attempts) {
        if(attempts >= 0) {
            DECODES_PER_AF = attempts;
        }

    }

    public int getDecoderAttemptsPerAFCycle() {
        return DECODES_PER_AF;
    }

    public void openCamera() {
        HSMLog.trace();

        try {
            if(!this.isCameraOpen.booleanValue()) {
                this.exploreCameraOptions();
                if(this.frontFacingCameraIndex >= 0 && this.rearFacingCameraIndex == -1) {
                    this.useFrontFacingCamera = Boolean.valueOf(true);
                } else if(this.frontFacingCameraIndex == -1 && this.rearFacingCameraIndex >= 0) {
                    this.useFrontFacingCamera = Boolean.valueOf(false);
                } else if(this.frontFacingCameraIndex == -1 && this.rearFacingCameraIndex == -1) {
                    return;
                }

                Boolean openedFrontCam = Boolean.valueOf(false);
                if(this.useFrontFacingCamera.booleanValue()) {
                    openedFrontCam = this.openFrontFacingCameraIfAvailable();
                }

                if(!openedFrontCam.booleanValue()) {
                    int numTries = 0;

                    while(numTries++ < 10) {
                        try {
                            this.camera = Camera.open();
                            break;
                        } catch (Exception var4) {
                            HSMLog.d("HSMLog", "Camera Open Fails = " + numTries);
                            Thread.sleep(1000L);
                        }
                    }

                    this.openCameraIndex = 0;
                }

                this.isCameraOpen = Boolean.valueOf(true);
                if(!this.isCameraDefaulted) {
                    this.setDefaultSettings();
                    this.isCameraDefaulted = true;
                } else {
                    this.applySavedCameraParameters();
                }
            }
        } catch (Exception var5) {
            HSMLog.e(var5);
        }

    }

    public void closeCamera() {
        HSMLog.trace();

        try {
            this.saveCameraParameters();
            if(this.camera != null) {
                this.camera.release();
                this.isCameraOpen = Boolean.valueOf(false);
            }
        } catch (Exception var2) {
            HSMLog.e(var2);
        }

    }

    public void reopenCamera() {
        HSMLog.trace();
        this.closeCamera();
        this.openCamera();
    }

    public Size getCurrentPreviewSize() {
        HSMLog.trace();
        return this.camera != null && this.isCameraOpen.booleanValue()?this.camera.getParameters().getPreviewSize():(this.currentPreviewSize != null?this.currentPreviewSize:null);
    }

    public void switchToFrontFacingCamera(Boolean switchToFront) {
        HSMLog.trace();
        if(switchToFront.booleanValue() && !this.useFrontFacingCamera.booleanValue()) {
            this.useFrontFacingCamera = Boolean.valueOf(true);
            this.reopenCamera();
        } else if(!switchToFront.booleanValue() && this.useFrontFacingCamera.booleanValue()) {
            this.useFrontFacingCamera = Boolean.valueOf(false);
            this.reopenCamera();
        }

    }

    public Boolean usingFrontCamera() {
        return this.useFrontFacingCamera;
    }

    public Size getOptimalPreviewSize() {
        HSMLog.trace();
        Parameters params = this.camera.getParameters();
        List<Size> supportedPreviewSizes = params.getSupportedPreviewSizes();
        Size optimalSize = (Size)supportedPreviewSizes.get(0);

        try {
            WindowManager winMan = (WindowManager)this.appContext.getSystemService("window");
            Display disp = winMan.getDefaultDisplay();
            Point screenRes = new Point(Math.max(disp.getWidth(), disp.getHeight()), Math.min(disp.getWidth(), disp.getHeight()));
            float screenAspectRatio = (float)screenRes.x / (float)screenRes.y;
            int minMPDelta = 2147483647;
            int HD_RES = 2073600;
            int VGA_RES = 307200;
            int MEGAPIXEL = 1000000;
            float arTolarance = 0.4F;
            int resTolarance = 78400;
            Iterator var14 = supportedPreviewSizes.iterator();

            while(var14.hasNext()) {
                Size s = (Size)var14.next();
                float aspectRatio = (float)s.width / (float)s.height;
                int res = s.width * s.height;
                int mpDelta = MEGAPIXEL - res;
                float arDelta = Math.abs(screenAspectRatio - aspectRatio);
                if(arDelta <= arTolarance && mpDelta >= resTolarance && res <= HD_RES && res >= VGA_RES && Math.abs(mpDelta) < minMPDelta) {
                    minMPDelta = Math.abs(mpDelta);
                    optimalSize = s;
                }
            }

            return optimalSize;
        } catch (Exception var20) {
            HSMLog.e(var20);
            return optimalSize;
        }
    }

    public boolean isFlashEnabled() {
        HSMLog.trace();
        return this.isFlashEnabled.booleanValue();
    }

    public void enableFlash(boolean enable) {
        HSMLog.trace();

        try {
            Parameters params = this.camera.getParameters();
            if(enable) {
                List<String> oSupportedFlash = params.getSupportedFlashModes();
                if(oSupportedFlash != null) {
                    if(oSupportedFlash.contains("torch")) {
                        params.setFlashMode("torch");
                        this.isFlashEnabled = Boolean.valueOf(true);
                    } else if(oSupportedFlash.contains("on")) {
                        params.setFlashMode("on");
                        this.isFlashEnabled = Boolean.valueOf(true);
                    } else {
                        this.isFlashEnabled = Boolean.valueOf(false);
                    }
                }
            } else {
                params.setFlashMode("off");
                this.isFlashEnabled = Boolean.valueOf(false);
            }

            this.camera.setParameters(params);
        } catch (Exception var4) {
            HSMLog.e(var4);
        }

    }

    public void setAutofocus(String autofocus) {
        try {
            Parameters params = this.camera.getParameters();
            params.setFocusMode(autofocus);
            this.camera.setParameters(params);
        } catch (Exception var3) {
            var3.printStackTrace();
        }

    }

    public int getLastImageWidth() {
        return this.lastImageWidth;
    }

    public int getLastImageHeight() {
        return this.lastImageHeight;
    }

    public byte[] getLastImage() {
        return this.baLastImage;
    }

    public void setLastImage(byte[] lastImage, int width, int height) {
        this.baLastImage = lastImage;
        this.lastImageWidth = width;
        this.lastImageHeight = height;
    }

    public void adjustCameraDisplayOrientation(int currentScreenRotation) {
        HSMLog.trace();

        try {
            int degrees = 0;
            switch(currentScreenRotation) {
                case 0:
                    degrees = 0;
                    break;
                case 1:
                    degrees = 90;
                    break;
                case 2:
                    degrees = 180;
                    break;
                case 3:
                    degrees = 270;
            }

            int result;
            if(VERSION.SDK_INT >= 9) {
                result = this.getDisplayOrientation(this.openCameraIndex, degrees);
            } else {
                result = (90 - degrees + 360) % 360;
            }

            this.camera.setDisplayOrientation(result);
        } catch (Exception var4) {
            HSMLog.e(var4);
        }

    }

    public void setFullFrameCallback(PictureCallback callback) {
        HSMLog.trace();
        this.fullPictureCallback = callback;
    }

    public void takeFullSizePicture() {
        HSMLog.trace();

        try {
            this.camera.takePicture((ShutterCallback)null, (PictureCallback)null, this.fullPictureCallback);
        } catch (Exception var2) {
            HSMLog.e(var2);
        }

    }

    public void killAutofocus() {
        try {
            this.camera.cancelAutoFocus();
            Parameters params = this.camera.getParameters();
            String currentFocusMode = params.getFocusMode();
            List<String> focusModes = params.getSupportedFocusModes();
            if(focusModes.contains("infinity")) {
                params.setFocusMode("infinity");
            } else if(focusModes.contains("continuous-video")) {
                params.setFocusMode("continuous-video");
            } else if(focusModes.contains("continuous-picture")) {
                params.setFocusMode("continuous-picture");
            }

            this.camera.setParameters(params);
            params.setFocusMode(currentFocusMode);
            this.camera.setParameters(params);
        } catch (Exception var4) {
            HSMLog.e(var4);
        }

    }

    private void saveCameraParameters() {
        try {
            if(this.camera != null) {
                if(this.useFrontFacingCamera.booleanValue()) {
                    this.savedFrontCameraSettings = this.camera.getParameters();
                } else {
                    this.savedRearCameraSettings = this.camera.getParameters();
                }
            }
        } catch (Exception var2) {
            HSMLog.e(var2);
        }

    }

    private void applySavedCameraParameters() {
        try {
            if(this.useFrontFacingCamera.booleanValue()) {
                if(this.savedFrontCameraSettings != null) {
                    this.camera.setParameters(this.savedFrontCameraSettings);
                }
            } else if(this.savedRearCameraSettings != null) {
                this.camera.setParameters(this.savedRearCameraSettings);
            }
        } catch (Exception var2) {
            HSMLog.e(var2);
        }

    }

    @TargetApi(9)
    private void setDefaultSettings() {
        HSMLog.trace();

        try {
            Parameters params = this.camera.getParameters();
            Size optimalSize;
            if(Build.MODEL.toLowerCase(Locale.ENGLISH).contains("glass")) {
                HSMLog.d("Google Glass Detected!");
                optimalSize = (Size)params.getSupportedPreviewSizes().get(0);
                params.setPreviewSize(optimalSize.width, optimalSize.height);
                this.currentPreviewSize = optimalSize;
                params.setPreviewFpsRange(30000, 30000);
            } else {
                optimalSize = this.getOptimalPreviewSize();
                params.setPreviewSize(optimalSize.width, optimalSize.height);
                this.currentPreviewSize = optimalSize;
                this.setHighestFrameRate(params);
                String isoVals = params.get("iso-values");
                if(isoVals != null) {
                    String[] vals = isoVals.split(",");
                    if(vals != null && vals.length > 0) {
                        String[] var5 = vals;
                        int var6 = vals.length;

                        for(int var7 = 0; var7 < var6; ++var7) {
                            String iso = var5[var7];
                            if(iso.equals("800") || iso.toUpperCase().equals("ISO800")) {
                                params.set("iso", iso);
                                break;
                            }
                        }
                    }
                }
            }

            this.setFocusArea(params);
            List<String> focusModes = params.getSupportedFocusModes();
            if(focusModes != null) {
                if(focusModes.contains("macro")) {
                    params.setFocusMode("macro");
                    this.isAutoFocusSupported = true;
                } else if(focusModes.contains("auto")) {
                    params.setFocusMode("auto");
                    this.isAutoFocusSupported = true;
                }
            }

            List<Integer> formats = params.getSupportedPreviewFormats();
            if(formats != null && formats.contains(Integer.valueOf(17))) {
                params.setPreviewFormat(17);
            }

            if(params.isZoomSupported()) {
                params.setZoom(0);
            }

            List<Size> supportedFullImageSizes = params.getSupportedPictureSizes();
            if(supportedFullImageSizes != null && supportedFullImageSizes.size() > 0) {
                Size largestSize = (Size)params.getSupportedPictureSizes().get(0);
                params.setPictureSize(largestSize.width, largestSize.height);
            }

            params.setJpegQuality(100);
            if(this.useFrontFacingCamera.booleanValue()) {
                this.savedFrontCameraSettings = params;
            } else {
                this.savedRearCameraSettings = params;
            }

            this.camera.setParameters(params);
        } catch (Exception var9) {
            HSMLog.e(var9);
        }

    }

    @TargetApi(9)
    private void setHighestFrameRate(Parameters params) {
        try {
            if(VERSION.SDK_INT < 9) {
                return;
            }

            List<int[]> frameRanges = params.getSupportedPreviewFpsRange();
            if(frameRanges != null) {
                int[] maxRange = (int[])frameRanges.get(frameRanges.size() - 1);
                params.setPreviewFpsRange(maxRange[0], maxRange[1]);
            }
        } catch (Exception var4) {
            HSMLog.e(var4);
        }

    }

    @TargetApi(14)
    private void setFocusArea(Parameters params) {
        try {
            if(VERSION.SDK_INT < 14) {
                return;
            }

            int maxFocusAreas = params.getMaxNumFocusAreas();
            if(maxFocusAreas > 0) {
                List<Area> areas = new ArrayList();
                Rect bounds = new Rect(-333, -333, 333, 333);
                areas.add(new Area(bounds, 1000));
                params.setFocusAreas(areas);
            }
        } catch (Exception var5) {
            HSMLog.e(var5);
        }

    }

    @TargetApi(9)
    private int getDisplayOrientation(int cameraId, int currentScreenRotationDegrees) {
        try {
            if(VERSION.SDK_INT < 9) {
                return -1;
            } else {
                CameraInfo info = new CameraInfo();
                Camera.getCameraInfo(cameraId, info);
                int result;
                if(info.facing == 1) {
                    result = (info.orientation + currentScreenRotationDegrees) % 360;
                    result = (360 - result) % 360;
                } else {
                    result = (info.orientation - currentScreenRotationDegrees + 360) % 360;
                }

                return result;
            }
        } catch (Exception var5) {
            HSMLog.e(var5);
            return -1;
        }
    }

    @TargetApi(9)
    private Boolean openFrontFacingCameraIfAvailable() {
        try {
            if(VERSION.SDK_INT < 9) {
                return Boolean.valueOf(false);
            } else {
                for(int i = 0; i < Camera.getNumberOfCameras(); ++i) {
                    CameraInfo info = new CameraInfo();
                    Camera.getCameraInfo(i, info);
                    if(info.facing == 1) {
                        this.camera = Camera.open(i);
                        this.openCameraIndex = i;
                        return Boolean.valueOf(true);
                    }
                }

                return Boolean.valueOf(false);
            }
        } catch (Exception var3) {
            HSMLog.e(var3);
            return Boolean.valueOf(false);
        }
    }

    @TargetApi(9)
    private void exploreCameraOptions() {
        try {
            if(VERSION.SDK_INT < 9) {
                return;
            }

            for(int i = 0; i < Camera.getNumberOfCameras(); ++i) {
                CameraInfo info = new CameraInfo();
                Camera.getCameraInfo(i, info);
                if(info.facing == 1) {
                    this.frontFacingCameraIndex = i;
                } else if(info.facing == 0) {
                    this.rearFacingCameraIndex = i;
                }
            }
        } catch (Exception var3) {
            HSMLog.e(var3);
        }

    }
}
